package question16_20;

/**
 * @description:
 * @author: jhy
 * @time: 2021/5/22 13:53
 */
public class Question16 {

    public static void main(String[] args) {

    }

    //方法1：base为底，exponent为指数的数，
    //如果exponent为正数，就将base乘exponent次
    //如果exponent为负数，就将base乘exponent次再取倒数
    public double Power(double base, int exponent) {
        if(base == 0){
            return 0;
        }
        if(exponent == 0){
            return 1;
        }
        double res = 1.0;
        if(exponent > 0){
            for(int i = 0;i < exponent;i++){
                res *= base;
            }
            return res;
        }else {
            for(int i = 0;i < -exponent;i++){
                res *= base;
            }
            return 1/res;
        }
    }

    //32次方是16次方的平方
    public double Power2(double base, int exponent) {
        if(exponent == 0){
            return 1;
        }
        if(exponent == 1){
            return base;
        }

        double result = Power2(base,exponent >> 1);
        result *= result;
        if((exponent & 1) == 1){
            result *= base;
        }
        return result;

    }
}
